package com.jyy.prefabricated.controller;


import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.entity.ExamPaperAreaRule;
import com.jyy.prefabricated.service.ExamPaperAreaRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 考试试卷区域规则
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class ExamPaperAreaRuleController {

    @Autowired
    private ExamPaperAreaRuleService ruleService;

    /**
     * 新增规则
     * @param rule
     * @return
     */
    @PostMapping("/exam-paper-area-rule")
    public AjaxResult insert(@Valid @RequestBody ExamPaperAreaRule rule){
        log.info("Rest request to insert exam-paper-area-rule:{}",rule);
        int id = ruleService.insert(rule);
        ExamPaperAreaRule result = ruleService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 修改规则
     * @param rule
     * @return
     */
    @PutMapping("/exam-paper-area-rule")
    public AjaxResult update(@Valid @RequestBody ExamPaperAreaRule rule){
        log.info("Rest request to update exam-paper-area-rule:{}",rule);
        ruleService.update(rule);
        ExamPaperAreaRule result = ruleService.selectById(rule.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询规则详情
     * @param id
     * @return
     */
    @GetMapping("/exam-paper-area-rule/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find exam-paper-area-rule by id:{}",id);
        ExamPaperAreaRule result = ruleService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除规则
     * @param id
     * @return
     */
    @DeleteMapping("/exam-paper-area-rule/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete exam-paper-area-rule by id:{}",id);
        int row = ruleService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }


}
